package com.sixdog.regulate.service.impl;

import com.sixdog.regulate.bean.CmdResult;
import com.sixdog.regulate.cmdService.CmdUtils;
import com.sixdog.regulate.service.DeviceService;
import com.sixdog.regulate.utils.RegexUtil;
import com.sixdog.regulate.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import java.util.ArrayList;

@Service
public class DeviceServiceImpl implements DeviceService {
    private CmdUtils cmdUtils = new CmdUtils();


    //获取所有设备序列号
    @Override
    public ArrayList<String> getDeviceSerials(){
        StringBuilder command =  new StringBuilder().append("adb devices");
        //执行adb命令
        CmdResult cmdResult = cmdUtils.execute(command);
        //正则表达式
        String regex = "\\S*";
        ArrayList<String> deviceSerials = new ArrayList<>();
        //去除多余字符
        for (String item : cmdResult.getLines()) {
            String deviceSerial = RegexUtil.cutPattern(regex, item);
            if (deviceSerial.equals("List") || StringUtils.isBlank(deviceSerial)){
                continue;
            }
            deviceSerials.add(deviceSerial);
        }
        return deviceSerials;
    }
    //获取设备型号
    @Override
    public String getDeviceModel(String deviceSerial){
        StringBuilder command =  new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out getprop ro.product.model");
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult.getLines().get(0);
    }
    //获取设备品牌
    @Override
    public String getDeviceBrand(String deviceSerial){
        StringBuilder command =  new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out getprop ro.product.brand");
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult.getLines().get(0);
    }
    //获取设备屏幕分辨率
    @Override
    public String getPhysicalSize(String deviceSerial){
        StringBuilder command =  new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out wm size");
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult.getLines().get(0);
    }
    //获取设备屏幕dpi
    @Override
    public String getDeviceDpi(String deviceSerial){
        StringBuilder command =  new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out wm density");
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult.getLines().get(0);
    }
    //获取设备安卓id
    @Override
    public String getAndroidId(String deviceSerial){
        StringBuilder command =  new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out settings get secure android_id");
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult.getLines().get(0);
    }
    //获取安卓版本
    @Override
    public String getAndroidVersion(String deviceSerial){
        StringBuilder command =  new StringBuilder("adb -s ").append(deviceSerial).append(" exec-out getprop ro.build.version.release");
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult.getLines().get(0);
    }

    //重启设备
    @Override
    public CmdResult rebootDevice(String deviceSerial){
        StringBuilder command =  new StringBuilder().append("adb -s ").append(deviceSerial).append(" reboot");
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult;
    }
    //关闭设备
    @Override
    public CmdResult shutDownDevice(String deviceSerial){
        StringBuilder command =  new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out reboot -p");
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult;
    }

    //进入设备shell会话(暂时没用)
    @Override
    public CmdResult openDeviceShell(String deviceSerial){
        StringBuilder command = new StringBuilder().append("adb -s ").append(deviceSerial).append(" shell");
        CmdResult cmdResult = cmdUtils.execute(command,CmdUtils.EXECUTE_TYPE_MAINTAIN);
        return cmdResult;
    }
    //获取自动调节亮度模式是否开启
    @Override
    public boolean getScreenBrightnessMode(String deviceSerial) {
        StringBuilder command = new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out settings get system screen_brightness_mode");
        CmdResult cmdResult = cmdUtils.execute(command);
        String result =cmdResult.getLines().get(0);
        if (result=="1"){
            return true;
        }
        return false;
    }
    //关闭或开启自动调节亮度模式
    @Override
    public CmdResult setScreenBrightnessMode(String deviceSerial, boolean state) {
        StringBuilder command = new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out settings put system screen_brightness_mode ");
        if (state){
           command.append("1");
        }else {
           command.append("0");
        }
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult;
    }
    //获取设备屏幕亮度(最大值并非100)
    @Override
    public int getScreenBrightness(String deviceSerial) {
        StringBuilder command = new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out settings get system screen_brightness");
        CmdResult cmdResult = cmdUtils.execute(command);
        int result = Integer.parseInt(cmdResult.getLines().get(0));
        return result;
    }
    //设置设备屏幕亮度(最大值并非100)
    @Override
    public CmdResult setScreenBrightness(String deviceSerial, int value) {
        StringBuilder command = new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out settings put system screen_brightness ").append(value);
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult;
    }


}
